package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;

import edu.pku.sei.modeler.editpolicies.SubjectNodeEditPolicy;
import edu.pku.sei.modeler.figures.SubjectFigure;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.structure.StructurePackage;
import edu.pku.sei.modeler.model.uml.Kernel.Classifier;
import edu.pku.sei.modeler.uml.commands.ChangeNameCommand;



public class UMLSubjectEditpart extends AbstractUMLNodeEditPart {

	public UMLSubjectEditpart(MetaShapeElement model) {
		super(model);
	}

	private Classifier getModelElement() {
		return (Classifier)((MetaNode) getModel()).getModelElement();
	}
	

	@Override
	protected DirectEditPolicy createDirectEditPolicy() {
		return new DirectEditPolicy() {
			protected Command getDirectEditCommand(DirectEditRequest request) {
				return new ChangeNameCommand(getModelElement(),
						(String) request.getCellEditor().getValue());
			}

			protected void showCurrentEditValue(DirectEditRequest request) {
				IFigure fig = getDirectEditFigure();
				if (fig instanceof Label) {
					((Label) fig).setText(getModelElement().getName());
					fig.getUpdateManager().performUpdate();
				}
			}
		};

	}
	
	@Override
	protected IFigure getDirectEditFigure() {
		return null;
	}

	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new SubjectNodeEditPolicy());
	}

	@Override
	protected IFigure createFigure() {
		return new SubjectFigure();
	}

	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		this.refreshVisuals();
		super.handlePropertyChanged(event);
	}

	@Override
	protected void refreshVisuals() {
		((SubjectFigure)this.getFigure()).setName(this.getModelElement().getName());
		super.refreshVisuals();
	}
	
	
	@Override
	public IFigure getContentPane() {
		if (getFigure() instanceof SubjectFigure) {
			return ((SubjectFigure) getFigure()).getContentPane();
		}

		return super.getFigure();
	}

}
